//
//  AIMineController.m
//  ZJCLoveBee
//
//  Created by 朱建超 on 16/12/21.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

#import "AIMineController.h"
#import "Masonry.h"
#import "ZJCSettingViewController.h"
#import "AIMineThreeButtonView.h"
#import "AIMineThreeButtonCell.h"
#import "AIMineMyOrderController.h"
#import "AIMineYouHuiQuanController.h"
#import "AIMineMessageController.h"

//图片的高度
#define BackViewHeight 160
#define HeadImageHeight 66.5
#define SetImageHeight 23.5

@interface AIMineController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property(nonatomic,weak)UIImageView *iconView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,weak)UIButton *setButton;

//@property(nonatomic,weak)AIMineThreeButtonView *threeButtonView;

@end

static NSString *threeButtonCellID = @"threeButtonCellID";
static NSString *cellID = @"cellID";

@implementation AIMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBarHidden = YES;
    
    //1、创建登录背景图
    [self setupImageView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToController:) name:@"goToController" object:nil];
    
    [self addPanGesture];
    
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    
    if (animated) {
        
        self.navigationController.navigationBarHidden = YES;
        
    }

}

#pragma mark - 控制器跳转的通知
-(void)goToController:(NSNotification *)notification{
    
    AIMineMyOrderController *myOrderController = [[AIMineMyOrderController alloc] init];
    AIMineYouHuiQuanController *youHuiQuanController = [[AIMineYouHuiQuanController alloc] init];
    AIMineMessageController *messageController = [[AIMineMessageController alloc] init];
    
        switch ([notification.userInfo[@"btnTag"] integerValue]) {
            case 0:
                [self.navigationController pushViewController:myOrderController animated:YES];
                self.navigationController.navigationBarHidden = NO;
                break;
            case 1:
                [self.navigationController pushViewController:youHuiQuanController animated:YES];
                self.navigationController.navigationBarHidden = NO;
                break;
            case 2:
                [self.navigationController pushViewController:messageController animated:YES];
                self.navigationController.navigationBarHidden = NO;
                break;
        }

}

#pragma mark - 创建登录背景图
-(void)setupImageView{

    //背景图
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
    
    [self.view addSubview:imageView];
    
    
    _iconView = imageView;
    
    //设置imageView的填充模式
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;

    //约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.height.mas_equalTo(BackViewHeight);
    }];
    
        
    //头像图片
    
    UIImageView *headImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
    
    [self.iconView addSubview:headImage];
    
    
    //设置imageView的填充模式
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    //约束
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_top).offset(40);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.with.mas_equalTo(HeadImageHeight);
    }];
    
    
    //联系方式
    {
        UILabel *contactLabel = [[UILabel alloc] init];
        
        contactLabel.text = @"13585968922";
        contactLabel.textColor = [UIColor whiteColor];
        
        [self.view addSubview:contactLabel];
        
        [contactLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(headImage.mas_centerX);
            make.top.mas_equalTo(headImage.mas_bottom).offset(15);
            
        }];
    }
    
    //创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView registerClass:[AIMineThreeButtonCell class] forCellReuseIdentifier:threeButtonCellID];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.estimatedRowHeight = 200;
    tableView.tableFooterView = [[UIView alloc] init];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.mas_equalTo(self.view);
        make.top.mas_equalTo(imageView.mas_bottom);
        
    }];
    
    imageView.userInteractionEnabled = YES;
        
    //设置按钮
    UIButton *setButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [imageView addSubview:setButton];
    
    self.setButton = setButton;
    
    [setButton setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
    
    //约束
    [self.setButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_top).offset(20);
        make.right.mas_equalTo(imageView.mas_right).offset(-20);
        make.height.with.mas_equalTo(SetImageHeight);
    }];
    
    [setButton addTarget:self action:@selector(gotoSettingController) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }else if (section == 1){
        return 10;
    }else if (section == 2){
        return 10;
    }else{
        return 10;
    }

}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else if (section == 2){
        return 1;
    }else{
        return  2;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        
        AIMineThreeButtonCell *threeButtonCell = [tableView dequeueReusableCellWithIdentifier:threeButtonCellID forIndexPath:indexPath];
        
        return threeButtonCell;
        
    }else if (indexPath.section == 1){
    
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"我的收货地址";
            cell.imageView.image = [UIImage imageNamed:@"v2_my_address_icon"];
            
        }else{
        
            cell.textLabel.text = @"我的店铺";
            cell.imageView.image = [UIImage imageNamed:@"icon_mystore"];
        
        }
        
        return cell;
    
    }else if (indexPath.section == 2){
    
        cell.textLabel.text = @"把爱鲜蜂分享给朋友";
        cell.imageView.image = [UIImage imageNamed:@"v2_my_share_icon"];
        
        return cell;
    
    }else{
    
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"客服帮助";
            cell.imageView.image = [UIImage imageNamed:@"online_service"];
            
        }else{
        
            cell.textLabel.text = @"意见反馈";
            cell.imageView.image = [UIImage imageNamed:@"v2_my_feedback_icon"];
        
        }
        
        return cell;
    
    }
    
}

#pragma MARK - 跳转到设置界面
-(void)gotoSettingController{
    
    ZJCSettingViewController *setController = [ZJCSettingViewController new];
    
    [self.navigationController pushViewController:setController animated:YES];

}

#pragma mark - 手势的代理方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
    
}

#pragma mark - 添加拖拽手势
-(void)addPanGesture{
    
    UIPanGestureRecognizer *panGrsture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    
    panGrsture.delegate = self;
    
    self.navigationController.navigationBarHidden = YES;
    
    [self.view addGestureRecognizer:panGrsture];
}

#pragma mark - 拖拽手势的方法
-(void)panGesture:(UIPanGestureRecognizer *)panGesture{
    
    //获取移动的距离
    CGPoint movePoint = [panGesture translationInView:self.view];
    
    //注意恢复
    [panGesture setTranslation:CGPointZero inView:panGesture.view];
    
    //更新约束mas_update
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        //计算差值
        CGFloat chaValue = self.iconView.bounds.size.height + movePoint.y;
        
        if (chaValue < 0) {
            
            //一旦用力过猛，小于64，直接把iamgeView的高度设置为64
            make.height.mas_equalTo(0);
            
            return;
            
        }else if (chaValue > BackViewHeight){
            
            make.height.mas_equalTo(BackViewHeight) ;
            
            return;
            
        }
        
        make.height.mas_equalTo(chaValue);
        
    }];
    
    
}

@end
